package com.example.jf.jfapp;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.jf.jfapp.bean.response.SendCommentRequestBean;
import com.example.jf.jfapp.bean.response.SendCommentResponse;
import com.example.jf.jfapp.utils.AppConfigUtils;
import com.example.jf.jfapp.utils.Constant;
import com.example.jf.jfapp.utils.OkHttpUtils;
import com.example.jf.jfapp.utils.RequestUtils;
import com.google.gson.Gson;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.jf.jfapp.utils.OkHttpUtils.getOkHttpClient;

/**
 * 评论编辑
 *
 * 评论成功，返回数据给上一个界面，判断是否需要刷新（也可以自己造数据，这样避免再次访问网络）
 * 显示最新的评论
 */

public class BlogCommentEditActivity extends AppCompatActivity {
    public final static int COMMENT_RESULT_CODE = 222;
    public final static String COMMENT_STATU = "statu";
    private static final String TAG ="CommentEditActivity";
    public static final String STATU_ID_KEY = "STATU_ID_KEY";

    public SendCommentRequestBean mSendCommentRequestBean;


    @BindView(R.id.et_send_content)
    AppCompatEditText etSendContent;
    private String BlogID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_comment);


        BlogID = getIntent().getStringExtra(STATU_ID_KEY);


        ButterKnife.bind(this);

    }


    @OnClick({R.id.tv_cancle, R.id.tv_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancle:
                finish();
                break;
            case R.id.tv_send:
                String content=etSendContent.getText().toString();
                if (content.equals("")) {
                    Toast.makeText(BlogCommentEditActivity.this, "评论不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    String userId=AppConfigUtils.getInstance().getLoginInfo(this).getObjectId();
                    if(userId==""||userId.isEmpty()||userId==null){
                        Toast.makeText(this, "请先登录！", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(this,LoginActivity.class);
                        startActivity(intent);
                        return;
                    }
                    mSendCommentRequestBean = new SendCommentRequestBean();
                    mSendCommentRequestBean.setUserId(userId);

                    mSendCommentRequestBean.setArticleId(BlogID);
                    mSendCommentRequestBean.setContent(etSendContent.getText().toString());
                    sendComment();
                    Toast.makeText(BlogCommentEditActivity.this, "提交评论中...", Toast.LENGTH_SHORT).show();

                }
                finish();
                break;
        }
    }


    private void sendComment() {

//        final Request request = requestPOST(mBlogCommentRequest, Constant.URL.BLOG_COMMENT);
//        OkHttpClient okHttpClient = getOkHttpClient();
        Request request = RequestUtils.requestPOST(mSendCommentRequestBean, Constant.URL.BLOG_EDIT_COMMENT);


        OkHttpClient okHttpClient = getOkHttpClient();


        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(BlogCommentEditActivity.this, "评论失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "请求发送评论 " + result);
                Log.e(TAG, "请求发送评论:    "+response);




            }
        });
    }




}
